package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.mapper.SupplierMapper;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("SupplierService")
public class SupplierServiceImpl implements SupplierService {
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public List<Supplier> findAll() {
        try {
            return supplierMapper.findAll();
        }catch (Exception e){
            LOGGER.error("SupplierServiceImpl.findAll()方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public Supplier findById(Long id) throws Exception {
        try {
            return supplierMapper.findById(id);
        }catch (Exception e){
            LOGGER.error("SupplierServiceImpl.findById()方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public Page<Supplier> findPage(Supplier entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<Supplier> page=null;
        try{
            page=new Page<Supplier>();
            page.setPageSize(pageSize);
            Long count= supplierMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);

            List<Supplier> data=supplierMapper.getPage(entity,page.offset(),page.getPageSize());

            page.setData(data);
        }catch (Exception e){
            LOGGER.error("SupplierServiceImpl.findPage(StorageRecord.Integer,Integer)方法出现异常。",e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(Supplier entity) throws Exception {
        try {
            if (entity.getId()==null){
                return supplierMapper.insert(entity)>0;
            }else {
                return supplierMapper.update(entity)>0;
            }
        }catch (Exception e){
            LOGGER.error("SupplierServiceImpl.saveOrUpdate(SysUser)方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public Supplier findBySupCode(String supCode) throws Exception {
        try {
            return supplierMapper.getBySupCode(supCode);
        }catch (Exception e){
            LOGGER.error("SupplierServiceImpl.findBySupCode(String)方法出现异常",e);
            throw e;
        }
    }

    @Override
    public int deleteById(Long id) throws Exception {
        try {
            return supplierMapper.deleteById(id);
        }catch (Exception e){
            LOGGER.error("SupplierServiceImpl.deleteById(Long)方法出现异常",e);
            throw e;
        }
    }
}
